//
//  DownloadRecvData.m
//  mcf
//
//  Created by yuike6 yuike6 on 12-2-8.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "DownloadRecvData.h"
#import "header.h"

@implementation DownloadRecvData
@synthesize  name = name_;
@synthesize localCacheDirPath = localCacheDirPath_;

@synthesize needRecvDataLenght = needRecvDataLenght_;
@synthesize valid = valid_;

@synthesize bCached;
@synthesize bCheckRecvDataLenght;

@synthesize downloadModel = downloadModel_;
@synthesize recvFinished;


-(void) dealloc
{    
    YRelease(name_);
    YRelease(localCacheDirPath_);
    
    YRelease(downloadModel_)
    [super dealloc];
}

-(id) init
{
    self = [super init];
    if (self) 
    {
        self.bCheckRecvDataLenght = YES;
        [self setRecvFinished:FALSE];
    }
    return self;
}

-(void) AppendData:(NSData *)data
{
    // 子类 override 此方法
    kMustOverrideMethod
}

- (long long)length
{
    // 子类 override 此方法
    kMustOverrideMethod
    return 0;
}

- (NSData *) data
{
    // 子类 override 此方法
    kMustOverrideMethod
    return nil;
}

- (void) clearData
{
    // 子类 override 此方法
    kMustOverrideMethod
}

- (void) processRecvAllDataFinish
{
    kMustOverrideMethod
}
- (void) processRecvAllDataFalied
{
    kMustOverrideMethod
}
@end

@implementation DownloadRecvDataMemory
@synthesize recvData = data_;

-(void) dealloc
{
    YRelease(data_);
    [super dealloc];
}

-(id) init
{
    self = [super init];
    if(self)
    {
        self.recvData = [NSMutableData data];
    }
    return self;
}

-(void) AppendData:(NSData *)dataValue
{
    if(self.recvData == nil)
        self.recvData = [[[NSMutableData alloc] init] autorelease];
    [self.recvData appendData:dataValue];
    
}

- (long long)length
{
    if(self.data == nil)
    {
        return 0;
    }
    
    return self.data.length;
}

- (NSData *) data
{
    return data_;
}

- (void) processRecvAllDataFinish
{
    
}
- (void) processRecvAllDataFalied
{

}
@end


@implementation DownloadRecvDataFileSystem

@synthesize localFullPath = localFullPath_;
@synthesize fileHandle = file_handle_;

@synthesize tmpFullPath = tmpFullPath_;

-(void) dealloc
{
    [self.fileHandle closeFile];      
    YRelease(file_handle_);    
    
    if (!self.isRecvFinished) 
    {
        [self clearData];
    }
    YRelease(localFullPath_);  
    YRelease(tmpFullPath_);
    [super dealloc];
}

- (id) init
{
    self = [super init];
    if (self) {
        isCraeteByMyself = NO;
        self.localFullPath = Nil;
        self.tmpFullPath = Nil;
        
        if (![YFile fileExistsAtPath:kDownloadTempPath]) {
            [YFile createDirectoryAtPath:kDownloadTempPath withAttributes:nil];
        } 
    }
    return self;
}

- (void) clearData
{
    if (![NSString isNilOrEmpty:self.localFullPath] &&
        [YFile fileExistsAtPath:self.localFullPath]) {
//        DebugLog(@"clear file: %@", self.localFullPath);
//        [YFile deleteFileAtPath:self.localFullPath];
    }
    
    if (![NSString isNilOrEmpty:self.tmpFullPath] &&
        [YFile fileExistsAtPath:self.tmpFullPath]) 
    {
//        DebugLog(@"clear tempfile: %@",  self.tmpFullPath);
        [YFile deleteFileAtPath:self.tmpFullPath];
    }    
}

-(void) AppendData:(NSData *)data
{   
    
    @try 
    {
        if ([NSString isNilOrEmpty:self.tmpFullPath])
        {
            // 文件名为空，无法保存，直接退出
            DebugLog(@"Download File is Nil, return");
            return;
        }        
                
        
        if(![YFile fileExistsAtPath:self.tmpFullPath])
        {
            [YFile createFileAtPath:self.tmpFullPath withData:nil andAttributes:nil];
            isCraeteByMyself = YES;
            
            if (!self.fileHandle) 
            {
                [self.fileHandle closeFile];
                YRelease(file_handle_);            
            }
            self.fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:self.tmpFullPath];  
        }    
        
        if (!isCraeteByMyself) {
            return;
        }
        
        
        if (!self.fileHandle) {
            DebugLog(@"fileHandle is Nil, return");
            return;
        }
        
        unsigned long long  seekOfFile = [self.fileHandle seekToEndOfFile];
        if (seekOfFile < 0.0f) 
        {
            return;
        }    
        [self.fileHandle writeData:data];  
    }
    @catch (NSException *exception) {
        DebugLog(@"Error: name: %@ reason: ", exception.name, exception.reason);
        return;
    }
    @finally {
        return;
    }    
}

- (long long)length
{
    @try {
        unsigned long long length_ = [YFile sizeOfFilePath:self.tmpFullPath];
        return  length_;
    }
    @catch (NSException *exception) {
        DebugLog(@"Error: name: %@ reason: ", exception.name, exception.reason);
        return 0.0f;
    }
    @finally {
        
    }
    
}

- (NSString *) tmpFullPath
{    
    if ([NSString isNilOrEmpty:tmpFullPath_]) {
        tmpFullPath_ = [[YPath Combine:kDownloadTempPath combinePath:[self.name stringByAppendingString:[NSString generateUUID]]] retain];  
        
        BOOL bExists = [YFile fileExistsAtPath:tmpFullPath_];
        if (bExists) {
            [YFile deleteFileAtPath:tmpFullPath_];
        }
    }
    
    return tmpFullPath_;
}

- (NSData *) data
{    
    NSURL * file_url_ = [NSURL fileURLWithPath:self.localFullPath];
    if(!file_url_)
    {
        return nil;
    }
    NSData * data_ = [NSData dataWithContentsOfURL:file_url_];
    return data_;
}

- (BOOL) bCached
{
    BOOL bExists = [YFile fileExistsAtPath:self.localFullPath];
    return bExists;
}


- (void) processRecvAllDataFinish
{
    if ([NSString isNilOrEmpty:self.localFullPath] ||
        [NSString isNilOrEmpty:self.tmpFullPath]) 
    {
        return;
    }
    
    [YFile moveFileFromPath:self.tmpFullPath toPath:self.localFullPath];
}
- (void) processRecvAllDataFalied
{
    
}

@end
